<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Find whether to send the query to the master, the slave or the last used MySQL server</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.mysqlnd-ms-match-wild.html">mysqlnd_ms_match_wild</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.mysqlnd-ms-set-qos.html">mysqlnd_ms_set_qos</a></div>
 <div class="up"><a href="ref.mysqlnd-ms.html">Mysqlnd_ms 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.mysqlnd-ms-query-is-select" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mysqlnd_ms_query_is_select</h1>
  <p class="verinfo">(PECL mysqlnd_ms &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">mysqlnd_ms_query_is_select</span> &mdash; <span class="dc-title">Find whether to send the query to the master, the slave or the last used MySQL server</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.mysqlnd-ms-query-is-select-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> 
   <span class="methodname"><strong>mysqlnd_ms_query_is_select</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span>
   )</div>

  <p class="para rdfs-comment">
   Finds whether to send the query to the master, the slave or the
   last used MySQL server.
  </p>
  <p class="para">
   The plugins built-in read/write split mechanism
   will be used to analyze the query string to make a recommendation where
   to send the query. The built-in read/write split mechanism is very
   basic and simple. The plugin will recommend sending all queries to the
   MySQL replication master server but those which begin with
   <em>SELECT</em>, or begin with a SQL hint which
   enforces sending the query to a slave server. Due to the basic
   but fast algorithm the plugin may propose to run some read-only
   statements such as <em>SHOW TABLES</em> on the replication master.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mysqlnd-ms-query-is-select-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>
<em><code class="parameter">query</code></em></dt>

    <dd>

     <p class="para">
     Query string to test.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mysqlnd-ms-query-is-select-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   A return value of <strong><code>MYSQLND_MS_QUERY_USE_MASTER</code></strong>
   indicates that the query should be send to the MySQL replication
   master server. The function returns a value of
   <strong><code>MYSQLND_MS_QUERY_USE_SLAVE</code></strong> if the query can be run
   on a slave because it is considered read-only. A value of
   <strong><code>MYSQLND_MS_QUERY_USE_LAST_USED</code></strong> is returned to recommend
   running the query on the last used server. This can either be a MySQL
   replication master server or a MySQL replication slave server.
  </p>
  <p class="para">
   If read write splitting has been disabled by setting
   <em>mysqlnd_ms.disable_rw_split</em>, the function will
   always return <strong><code>MYSQLND_MS_QUERY_USE_MASTER</code></strong> or
   <strong><code>MYSQLND_MS_QUERY_USE_LAST_USED</code></strong>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mysqlnd-ms-query-is-select-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1813">
    <p><strong>Example #1 <span class="function"><strong>mysqlnd_ms_query_is_select()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">is_select</span><span style="color: #007700">(</span><span style="color: #0000BB">$query</span><span style="color: #007700">)<br />{<br />&nbsp;switch&nbsp;(</span><span style="color: #0000BB">mysqlnd_ms_query_is_select</span><span style="color: #007700">(</span><span style="color: #0000BB">$query</span><span style="color: #007700">))<br />&nbsp;{<br />&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">MYSQLND_MS_QUERY_USE_MASTER</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"'%s'&nbsp;should&nbsp;be&nbsp;run&nbsp;on&nbsp;the&nbsp;master.\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">MYSQLND_MS_QUERY_USE_SLAVE</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"'%s'&nbsp;should&nbsp;be&nbsp;run&nbsp;on&nbsp;a&nbsp;slave.\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">MYSQLND_MS_QUERY_USE_LAST_USED</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"'%s'&nbsp;should&nbsp;be&nbsp;run&nbsp;on&nbsp;the&nbsp;server&nbsp;that&nbsp;has&nbsp;run&nbsp;the&nbsp;previous&nbsp;query\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;default:<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"No&nbsp;suggestion&nbsp;where&nbsp;to&nbsp;run&nbsp;the&nbsp;'%s',&nbsp;fallback&nbsp;to&nbsp;master&nbsp;recommended\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;break;<br />&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">is_select</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;test(id)&nbsp;VALUES&nbsp;(1)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">is_select</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;1&nbsp;FROM&nbsp;DUAL"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">is_select</span><span style="color: #007700">(</span><span style="color: #DD0000">"/*"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">MYSQLND_MS_LAST_USED_SWITCH&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"*/SELECT&nbsp;2&nbsp;FROM&nbsp;DUAL"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
INSERT INTO test(id) VALUES (1) should be run on the master.
SELECT 1 FROM DUAL should be run on a slave.
/*ms=last_used*/SELECT 2 FROM DUAL should be run on the server that has run the previous query
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mysqlnd-ms-query-is-select-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member">
     <a href="mysqlnd-ms.constants.html" class="link">Predefined Constants</a>
    </li>
    <li class="member">
     <a href="mysqlnd-ms.plugin-ini-json.html#ini.mysqlnd-ms-plugin-config-v2.filter-user" class="link"><em>user</em></a> filter
    </li>
   </ul>
      <ul class="simplelist">
    <li class="member">
     <a href="mysqlnd-ms.configuration.html" class="link">Runtime configuration</a>
    </li>
    <li class="member">
     <a href="mysqlnd-ms.configuration.html#ini.mysqlnd-ms.disable-rw-split" class="link">mysqlnd_ms.disable_rw_split</a>
    </li>
    <li class="member">
     <a href="mysqlnd-ms.configuration.html#ini.mysqlnd-ms.enable" class="link">mysqlnd_ms.enable</a>
    </li>
   </ul>

  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.mysqlnd-ms-match-wild.html">mysqlnd_ms_match_wild</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.mysqlnd-ms-set-qos.html">mysqlnd_ms_set_qos</a></div>
 <div class="up"><a href="ref.mysqlnd-ms.html">Mysqlnd_ms 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
